<?php
class Hotel_base_model extends CI_Model
{
    private $table   = 'hotel_base';
    private $table_2 = 'hotel_room';
    private $table_3 = 'room_price';
    private $table_4 = 'hotel_level';
    
    public function getHomeHotelList($params=array())
    {
        $this->db->select('hotel_base.hotel_id, hotel_base.hotel_name, hotel_base.provinceid, hotel_base.cityid, hotel_base.hotel_mark, hotel_base.level_id, hotel_base.hotel_pic, min(rp_shop_price) as rp_shop_price');
        $this->db->from($this->table.' AS hotel_base');
        $this->db->join($this->table_2.' AS hotel_room', 'hotel_base.hotel_id=hotel_room.hotel_id', 'INNER');
        $this->db->join($this->table_3.' AS room_price', 'room_price.room_id=hotel_room.room_id', 'INNER');
        if (!empty($params['provinceid'])) {
            $this->db->where('hotel_base.provinceid', $params['provinceid']);
        }
        if (!empty($params['cityid'])) {
            $this->db->where('hotel_base.cityid', $params['cityid']);
        }
        if (!empty($params['level_id'])) {
            $this->db->where('hotel_base.level_id', $params['level_id']);
        }
        $this->db->where('hotel_base.hotel_state', 1);
        $this->db->where('hotel_room.room_state', 1);
        $this->db->where('hotel_room.room_state', 1);
        $this->db->where('room_price.bargaining', 1);
        $this->db->where('room_price.rp_in_date <=', strtotime('today'));
        $this->db->group_by('hotel_base.hotel_id');
        $this->db->order_by('hotel_base.visit_count DESC');
        if (!empty($params['limit'])) {
            $this->db->limit($params['limit']);
        }
        return $this->db->get();
    }

    public function findHotelLevel($isArray = false)
    {
        $this->db->order_by('level_id', 'ASC');
        $result = $this->db->get($this->table_4);
        if ($isArray) {
            $rows = array();
            foreach ($result->result_array() as $row) {
                $rows[$row['level_id']] = $row;
            }
            return $rows;
        }
        return $result;
    }
}